package com.paul.sys.controller.back;

import com.paul.sys.model.Ptstaff;
import com.paul.sys.service.PtstaffService;
import com.paul.sys.util.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.security.auth.Subject;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by yingg_000 on 2015/7/13.
 */
@Controller
@RequestMapping("/back/")
public class LoginController {
    @Autowired
    private PtstaffService ptstaffService;


    @RequestMapping("loginPage")
    public String loginJSP(HttpServletRequest request) throws Exception {

        return "back/login";
    }

    @ResponseBody
    @RequestMapping("login")
    public Map<String, String> login(HttpServletRequest request) {
        SavedRequest savedRequest= WebUtils.getSavedRequest(request);
        String url=savedRequest==null?"":savedRequest.getRequestUrl();
        Map<String, String> result = new HashMap<String, String>();
        ParamUtil paramUtil = new ParamUtil(request);
        Map<String, String> param = null;
        try {
            param = paramUtil.getParamMap();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String pwd=TrafficMd5.md5(param.get("pwd"));
        String code=param.get("code");
        UsernamePasswordToken token = new UsernamePasswordToken(code,pwd);
        token.setRememberMe(true);
        org.apache.shiro.subject.Subject subject= SecurityUtils.getSubject();
        try {

            subject.login(token);
        } catch (UnknownAccountException ex) {//用户名没有找到
            ex.printStackTrace();
        } catch (IncorrectCredentialsException ex) {//用户名密码不匹配
            ex.printStackTrace();
        }catch (AuthenticationException e) {//其他的登录错误
            e.printStackTrace();
        }

//        pwd= TrafficMd5.md5(pwd);
//        param.replace("pwd",pwd);
//
//        Ptstaff ptstaff = ptstaffService.selectByCodeAndPass(param);
//
//        if (ptstaff != null) {
//            //判断是否已登录

//            //登陆成功
//            CommSession.setUser(request, ptstaff);
//            result.put("success", "true");
//        } else {  //登录失败
//            result.put("success", "false");
//        }
//        return result;
        //验证是否成功登录的方法
        if (subject.isAuthenticated()) {

            result.put("success", "true");
            result.put("url",url);
            return result;
        }
        result.put("success", "false");
        return result;
    }

    @RequestMapping("logout")
    public String loginout(HttpServletRequest request) throws Exception {
        SecurityUtils.getSubject().logout();
//        CommSession.removeUser(request);
//        CommSession.removeSession(request);
        // MapUtil.put("outtime", false);
        return "back/login";
    }
}
